//
//  OilCardCompanyView.m
//  Base_App
//
//  Created by Yutian Duan on 2019/6/19.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "OilCardCompanyView.h"
#import "YFBaseTextField.h"
#import "PlaceholderTextView.h"


@interface OilCardCompanyView () <UITextFieldDelegate> {
  NSArray *buttonArray;
}

@property (nonatomic, strong) UIButton *getCodeButton;

///@
@property (nonatomic, strong) PlaceholderTextView *textView;

@end

@implementation OilCardCompanyView

///! 获取验证码
- (UIButton *)getCodeButton {
  if (!_getCodeButton) {
    _getCodeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0 )];
    [self addSubview:_getCodeButton];
    _getCodeButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    
    [_getCodeButton setTitle:YTLocalizedString(@"获取验证码") forState:UIControlStateNormal];
    [_getCodeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _getCodeButton.layer.masksToBounds = YES;
    _getCodeButton.layer.cornerRadius = 8;
    _getCodeButton.backgroundColor = HEXCOLOR(0xbb2b25);
    [_getCodeButton addTarget:self action:@selector(sendCode) forControlEvents:UIControlEventTouchUpInside];
    
  }
  return _getCodeButton;
}



- (instancetype)initWithFrame:(CGRect)frame {
  self = [super initWithFrame: frame];
  if (self) {
    
    [self creatViews];
  }
  return self;
}

- (void)creatViews {
  
  CGFloat fontF = 13.0f;
  if (IS_IPHONE5) {
    fontF = 11.0f;
  }
  
  UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, self.yt_width - 30, 30)];
  titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0f];
  titleLabel.textColor = [UIColor blackColor];
  titleLabel.textAlignment = NSTextAlignmentLeft;
  titleLabel.text = YTLocalizedString(@"填写申请资料");
  [self addSubview:titleLabel];
  
  UILabel *hanNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.yt_left + 10, titleLabel.yt_bottom + 10, 85, 35)];
  hanNameLabel.textColor = [UIColor blackColor];
  hanNameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:fontF];
  hanNameLabel.text = [NSString stringWithFormat:@"%@:",YTLocalizedString(@"公司中文名称")];
  hanNameLabel.textAlignment = NSTextAlignmentLeft;
  [self addSubview:hanNameLabel];
  
  YFBaseTextField *hanNameField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(hanNameLabel.yt_right, hanNameLabel.yt_top, self.yt_width - hanNameLabel.yt_right - 100 - 20, hanNameLabel.yt_height)];
  [hanNameField setTextFont:hanNameLabel.font];
  [hanNameField set_placeTextColor:DEF_GRAY];
  hanNameField.placeholder = YTLocalizedString(@"输入公司中文全名");
  hanNameField.layer.masksToBounds = YES;
  hanNameField.layer.cornerRadius = 8;
  hanNameField.layer.borderWidth = 1.0f;
  hanNameField.layer.borderColor = COLOR_SEPARATOR.CGColor;
  hanNameField.leftViewMode = UITextFieldViewModeAlways;
  hanNameField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, hanNameLabel.yt_height)];
  [self addSubview:hanNameField];
  
  CGFloat buttonW = (self.yt_width - hanNameField.yt_right - 10)/2;
  
  UIButton *manButton = [[UIButton alloc] initWithFrame:CGRectMake(hanNameField.yt_right, hanNameField.yt_top, buttonW, hanNameField.yt_height)];
  [manButton setTitle:YTLocalizedString(@"先生") forState:UIControlStateNormal];
  manButton.titleLabel.font = [UIFont systemFontOfSize:fontF - 1];
  [manButton setTitleColor:DEF_GRAY forState:UIControlStateNormal];
  [manButton setTitleColor:DEF_BLACK forState:UIControlStateSelected];
  [manButton setImage:[UIImage imageNamed:@"Oil_checkPoint_01"] forState:UIControlStateNormal];
  [manButton setImage:[UIImage imageNamed:@"Oil_checkPoint_02"] forState:UIControlStateSelected];
  manButton.selected = YES;
  [manButton layoutButtonWithEdgeInsetsStyle:WWButtonEdgeInsetsStyleBottom imageTitleSpace:1];
  [self addSubview:manButton];
  
  UIButton *womanButton = [[UIButton alloc] initWithFrame:CGRectMake(manButton.yt_right, manButton.yt_top, manButton.yt_width, manButton.yt_height)];
  [womanButton setTitle:YTLocalizedString(@"女士") forState:UIControlStateNormal];
  womanButton.titleLabel.font = [UIFont systemFontOfSize:fontF - 1];
  [womanButton setTitleColor:DEF_GRAY forState:UIControlStateNormal];
  [womanButton setTitleColor:DEF_BLACK forState:UIControlStateSelected];
  [womanButton setImage:[UIImage imageNamed:@"Oil_checkPoint_01"] forState:UIControlStateNormal];
  [womanButton setImage:[UIImage imageNamed:@"Oil_checkPoint_02"] forState:UIControlStateSelected];
  [womanButton layoutButtonWithEdgeInsetsStyle:WWButtonEdgeInsetsStyleBottom imageTitleSpace:1];
  [self addSubview:womanButton];
  
  ///!
  UILabel *enNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(hanNameLabel.yt_left, hanNameLabel.yt_bottom + 10, hanNameLabel.yt_width, hanNameLabel.yt_height)];
  enNameLabel.textColor = [UIColor blackColor];
  enNameLabel.font = hanNameLabel.font;
  enNameLabel.text = [NSString stringWithFormat:@"%@:",YTLocalizedString(@"公司英文名称")];
  enNameLabel.textAlignment = NSTextAlignmentLeft;
  [self addSubview:enNameLabel];
  
  YFBaseTextField *enNameField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(enNameLabel.yt_right, enNameLabel.yt_top, self.yt_width - enNameLabel.yt_right - 10 , enNameLabel.yt_height)];
  [enNameField setTextFont:enNameLabel.font];
  [enNameField set_placeTextColor:DEF_GRAY];
  enNameField.placeholder = [NSString stringWithFormat:@"%@ %@",YTLocalizedString(@"输入公司英文全名"),@"eg. Chan Tai Man"];
  enNameField.layer.masksToBounds = YES;
  enNameField.layer.cornerRadius = 8;
  enNameField.layer.borderWidth = 1.0f;
  enNameField.layer.borderColor = COLOR_SEPARATOR.CGColor;
  enNameField.leftViewMode = UITextFieldViewModeAlways;
  enNameField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, hanNameLabel.yt_height)];
  [self addSubview:enNameField];

  ///! 商业登记证
  UILabel *businessCerLabel = [[UILabel alloc] initWithFrame:CGRectMake(enNameLabel.yt_left, enNameLabel.yt_bottom + 10, enNameLabel.yt_width, enNameLabel.yt_height)];
  businessCerLabel.textColor = [UIColor blackColor];
  businessCerLabel.font = hanNameLabel.font;
  businessCerLabel.text = [NSString stringWithFormat:@"%@:",YTLocalizedString(@"商业登记证")];
  businessCerLabel.textAlignment = NSTextAlignmentLeft;
  [self addSubview:businessCerLabel];
  
  YFBaseTextField *businessCerField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(businessCerLabel.yt_right, businessCerLabel.yt_top, enNameField.yt_width, businessCerLabel.yt_height)];
  [businessCerField setTextFont:enNameLabel.font];
  [businessCerField set_placeTextColor:DEF_GRAY];
  businessCerField.placeholder = YTLocalizedString(@"输入商业登记证前8位数字");
  businessCerField.layer.masksToBounds = YES;
  businessCerField.layer.cornerRadius = 8;
  businessCerField.layer.borderWidth = 1.0f;
  businessCerField.layer.borderColor = COLOR_SEPARATOR.CGColor;
  businessCerField.leftViewMode = UITextFieldViewModeAlways;
  businessCerField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, hanNameLabel.yt_height)];
  [self addSubview:businessCerField];

  ///! 有限公司 合伙公司 独资公司
  buttonW = businessCerField.yt_width/3;
  //! 有限公司
  UIButton *ltdButton = [[UIButton alloc] initWithFrame:CGRectMake(businessCerField.yt_left, businessCerField.yt_bottom + 10, buttonW, 30)];
  ltdButton.titleLabel.font = [UIFont systemFontOfSize:fontF];
  [ltdButton setTitle:YTLocalizedString(@"有限公司") forState:UIControlStateNormal];
  [ltdButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  [ltdButton setImage:[UIImage imageNamed:@"Oil_CheckImage_0"] forState:UIControlStateNormal];
  [ltdButton setImage:[UIImage imageNamed:@"Oil_CheckImage_1"] forState:UIControlStateSelected];
  [ltdButton addTarget:self action:@selector(changeCLick:) forControlEvents:UIControlEventTouchUpInside];
  [self addSubview:ltdButton];
  ltdButton.selected = YES;
  
  //! 合伙公司
  UIButton *partnerButton = [[UIButton alloc] initWithFrame:CGRectMake(ltdButton.yt_right, ltdButton.yt_top, ltdButton.yt_width, ltdButton.yt_height)];
  partnerButton.titleLabel.font = [UIFont systemFontOfSize:fontF];
  [partnerButton setTitle:YTLocalizedString(@"合伙公司") forState:UIControlStateNormal];
  [partnerButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  [partnerButton setImage:[UIImage imageNamed:@"Oil_CheckImage_0"] forState:UIControlStateNormal];
  [partnerButton setImage:[UIImage imageNamed:@"Oil_CheckImage_1"] forState:UIControlStateSelected];
  [partnerButton addTarget:self action:@selector(changeCLick:) forControlEvents:UIControlEventTouchUpInside];
//  partnerButton.yt_left = self.yt_centerX + 5;
  [self addSubview:partnerButton];

  ///! 独资公司
  UIButton *ownedButton = [[UIButton alloc] initWithFrame:CGRectMake(partnerButton.yt_right, partnerButton.yt_top, partnerButton.yt_width, partnerButton.yt_height)];
  ownedButton.titleLabel.font = [UIFont systemFontOfSize:fontF];
  [ownedButton setTitle:YTLocalizedString(@"独资公司") forState:UIControlStateNormal];
  [ownedButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  [ownedButton setImage:[UIImage imageNamed:@"Oil_CheckImage_0"] forState:UIControlStateNormal];
  [ownedButton setImage:[UIImage imageNamed:@"Oil_CheckImage_1"] forState:UIControlStateSelected];
  [ownedButton addTarget:self action:@selector(changeCLick:) forControlEvents:UIControlEventTouchUpInside];
//  ownedButton.yt_left = self.yt_centerX + 5;
  [self addSubview:ownedButton];
  buttonArray = @[ltdButton,partnerButton,ownedButton];
  
  
  ///1 手机号
  UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(businessCerLabel.yt_left, ltdButton.yt_bottom + 10, businessCerLabel.yt_width, businessCerLabel.yt_height)];
  phoneLabel.textColor = [UIColor blackColor];
  phoneLabel.font = hanNameLabel.font;
  phoneLabel.text = [NSString stringWithFormat:@"%@:",YTLocalizedString(@"手机号码")];
  phoneLabel.textAlignment = NSTextAlignmentLeft;
  [self addSubview:phoneLabel];
  
  YFBaseTextField *phoneField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(businessCerField.yt_left, phoneLabel.yt_top, businessCerField.yt_width/2 - 5, phoneLabel.yt_height)];
  [phoneField setTextFont:enNameLabel.font];
  [phoneField set_placeTextColor:DEF_GRAY];
  phoneField.placeholder = YTLocalizedString(@"输入联络手机号码");
  phoneField.layer.masksToBounds = YES;
  phoneField.layer.cornerRadius = 8;
  phoneField.layer.borderWidth = 1.0f;
  phoneField.layer.borderColor = COLOR_SEPARATOR.CGColor;
  phoneField.leftViewMode = UITextFieldViewModeAlways;
  phoneField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, hanNameLabel.yt_height)];
  [self addSubview:phoneField];
  
  self.getCodeButton.frame = CGRectMake(phoneField.yt_right + 10, phoneField.yt_top, phoneField.yt_width, phoneField.yt_height);
  
  
  UILabel *emailLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneLabel.yt_left, phoneLabel.yt_bottom + 10, phoneLabel.yt_width, phoneLabel.yt_height)];
  emailLabel.textColor = [UIColor blackColor];
  emailLabel.font = hanNameLabel.font;
  emailLabel.text = [NSString stringWithFormat:@"%@:",YTLocalizedString(@"电邮地址")];
  emailLabel.textAlignment = NSTextAlignmentLeft;
  [self addSubview:emailLabel];
  
  YFBaseTextField *emailField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(phoneField.yt_left, emailLabel.yt_top, phoneField.yt_width, phoneField.yt_height)];
  [emailField setTextFont:enNameLabel.font];
  [emailField set_placeTextColor:DEF_GRAY];
  emailField.placeholder = YTLocalizedString(@"输入电邮地址");
  emailField.layer.masksToBounds = YES;
  emailField.layer.cornerRadius = 8;
  emailField.layer.borderWidth = 1.0f;
  emailField.layer.borderColor = COLOR_SEPARATOR.CGColor;
  emailField.leftViewMode = UITextFieldViewModeAlways;
  emailField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, hanNameLabel.yt_height)];
  [self addSubview:emailField];
  
  YFBaseTextField *codeField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(emailField.yt_right + 10, emailField.yt_top, emailField.yt_width, emailField.yt_height)];
  [codeField setTextFont:enNameLabel.font];
  [codeField set_placeTextColor:DEF_GRAY];
  codeField.placeholder = YTLocalizedString(@"输入短讯验证码");
  codeField.layer.masksToBounds = YES;
  codeField.layer.cornerRadius = 8;
  codeField.layer.borderWidth = 1.0f;
  codeField.layer.borderColor = COLOR_SEPARATOR.CGColor;
  codeField.leftViewMode = UITextFieldViewModeAlways;
  codeField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, hanNameLabel.yt_height)];
  [self addSubview:codeField];
  
  
  
  UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(emailLabel.yt_left, emailLabel.yt_bottom + 10, emailLabel.yt_width, emailLabel.yt_height)];
  addressLabel.textColor = [UIColor blackColor];
  addressLabel.font = hanNameLabel.font;
  addressLabel.text = [NSString stringWithFormat:@"%@:",YTLocalizedString(@"香港住址")];
  addressLabel.textAlignment = NSTextAlignmentLeft;
  [self addSubview:addressLabel];
  
  _textView = [[PlaceholderTextView alloc] initWithFrame:CGRectMake(addressLabel.yt_right, addressLabel.yt_top, self.yt_width - addressLabel.yt_right - 10, 100)];
  _textView.placeholder = YTLocalizedString(@"输入完整香港邮寄地址");
  _textView.placeholderColor = DEF_GRAY;
  _textView.font = addressLabel.font;
  _textView.layer.masksToBounds = YES;
  _textView.layer.cornerRadius = 8;
  _textView.layer.borderWidth = 1.0f;
  _textView.layer.borderColor = COLOR_SEPARATOR.CGColor;
  [self addSubview:_textView];

  
  ///! 填写联络人
  UIView *midLineView = [[UIView alloc] initWithFrame:CGRectMake(titleLabel.yt_left, _textView.yt_bottom + 10, self.yt_width - titleLabel.yt_left * 2, 1)];
  midLineView.backgroundColor = DEF_GRAY;
  [self addSubview:midLineView];
  
  UILabel *contactTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.yt_left, midLineView.yt_bottom + 10, titleLabel.yt_width, titleLabel.yt_height)];
  contactTitleLabel.textColor = titleLabel.textColor;
  contactTitleLabel.font = titleLabel.font;
  contactTitleLabel.textAlignment = titleLabel.textAlignment;
  contactTitleLabel.text = YTLocalizedString(@"填写联络人资料");
  [self addSubview:contactTitleLabel];
  
  UILabel *contactLabel = [[UILabel alloc] initWithFrame:CGRectMake(hanNameLabel.yt_left, contactTitleLabel.yt_bottom + 10, hanNameLabel.yt_width, hanNameLabel.yt_height)];
  contactLabel.textColor = hanNameLabel.textColor;
  contactLabel.textAlignment = hanNameLabel.textAlignment;
  contactLabel.font = hanNameLabel.font;
  contactLabel.text = [NSString stringWithFormat:@"%@:",YTLocalizedString(@"联络人")];
  [self addSubview:contactLabel];
  
  ///!
  YFBaseTextField *contactNameField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(emailField.yt_left, contactLabel.yt_top, emailField.yt_width, emailField.yt_height)];
  [contactNameField setTextFont:enNameLabel.font];
  [contactNameField set_placeTextColor:DEF_GRAY];
  contactNameField.placeholder = YTLocalizedString(@"输入联络人姓名");
  contactNameField.layer.masksToBounds = YES;
  contactNameField.layer.cornerRadius = 8;
  contactNameField.layer.borderWidth = 1.0f;
  contactNameField.layer.borderColor = COLOR_SEPARATOR.CGColor;
  contactNameField.leftViewMode = UITextFieldViewModeAlways;
  contactNameField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, hanNameLabel.yt_height)];
  [self addSubview:contactNameField];
  
  YFBaseTextField *contactJobField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(codeField.yt_left, contactNameField.yt_top, codeField.yt_width, contactNameField.yt_height)];
  [contactJobField setTextFont:enNameLabel.font];
  contactJobField.delegate = self;
  [contactJobField set_placeTextColor:DEF_GRAY];
  contactJobField.placeholder = YTLocalizedString(@"请选择职位");
  contactJobField.layer.masksToBounds = YES;
  contactJobField.layer.cornerRadius = 8;
  contactJobField.layer.borderWidth = 1.0f;
  contactJobField.layer.borderColor = COLOR_SEPARATOR.CGColor;
  contactJobField.leftViewMode = UITextFieldViewModeAlways;
  contactJobField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, hanNameLabel.yt_height)];
  contactJobField.rightViewMode = UITextFieldViewModeAlways;
  UIImageView *redArrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 12)];
  redArrowImageView.image = [UIImage imageNamed:@"Oil_card_redArrow"];
  contactJobField.rightView = redArrowImageView;
  [self addSubview:contactJobField];

  ///! 联络人号码
  UILabel *contactNoLabel = [[UILabel alloc] initWithFrame:CGRectMake(contactLabel.yt_left, contactLabel.yt_bottom + 10, contactLabel.yt_width, contactLabel.yt_height)];
  contactNoLabel.textColor = contactLabel.textColor;
  contactNoLabel.font = contactLabel.font;
  contactNoLabel.text = [NSString stringWithFormat:@"%@:",YTLocalizedString(@"联络号码")];
  [self addSubview:contactNoLabel];
  
  YFBaseTextField *contactNoField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(contactNameField.yt_left, contactNoLabel.yt_top, enNameField.yt_width, enNameField.yt_height)];
  [contactNoField setTextFont:enNameLabel.font];
  [contactNoField set_placeTextColor:DEF_GRAY];
  contactNoField.placeholder = YTLocalizedString(@"输入联络号码");
  contactNoField.layer.masksToBounds = YES;
  contactNoField.layer.cornerRadius = 8;
  contactNoField.layer.borderWidth = 1.0f;
  contactNoField.layer.borderColor = COLOR_SEPARATOR.CGColor;
  contactNoField.leftViewMode = UITextFieldViewModeAlways;
  contactNoField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, hanNameLabel.yt_height)];
  [self addSubview:contactNoField];
  
  self.yt_height = contactNoField.yt_bottom + 10;

  
}

- (void)changeCLick:(UIButton *)sender {
  
  for (UIButton *button in buttonArray) {
    button.selected = NO;
  }
  sender.selected = YES;
  
}

- (void)sendCode {
  if (self.getCodeButton.selected == YES) {
    return;
  }
  
  [self countDownT];
  
}

- (void)countDownT {
  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  dispatch_block_t block = ^{
    int i = 60;
    while (i > 0) {
      if (i != 60) sleep(1);
      //耗时的操作
      NSLog(@"倒计时60秒----%d",i);
      i--;
      dispatch_async(dispatch_get_main_queue(), ^{
        if (self.getCodeButton) {
          if (i > 0) {
            // HEXCOLOR(0X888888)
            [self.getCodeButton setTitleColor:DEF_GRAY forState:UIControlStateNormal];
            [self.getCodeButton setTitle:[NSString stringWithFormat:@"%dS", i] forState:UIControlStateNormal];
            
            if (self.getCodeButton.selected == NO) {
              self.getCodeButton.selected = YES;
            }
            
          } else {
            [self.getCodeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.getCodeButton.selected = NO;
            [self.getCodeButton setTitle:YTLocalizedString(@"获取验证码") forState:UIControlStateNormal];
          }
        }
      });
    }
  };
  dispatch_async(queue, block);
}


#pragma makr - UITextFieldDelegate


@end
